﻿using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;

public class CommunityUI : MonoBehaviour
{
    public ListTogglesGroup topToggles;

    int m_Selection = -1;

    public CommunityPanel communityPanel;

    public ScrollRect scollViewRect;
    public RectTransform itemsParent;

    float m_lastInitTime = -100;

    public int getMomentsNumOneTime = 7;

    MomentsData m_Data;
    bool inited;
    void Awake()
    {
       
    }
    public void PreloadData()
    {
        RequestServerController.instance.ReqMomentsData(CommunityType.Recommand, 0, getMomentsNumOneTime, 0, (data) =>
        {
            if (!inited)
                m_Data = data;
        });
    }

    public void Init()
    {
        if (m_Selection == -1)
        {
            topToggles.Init(DoOnSelectTopToggle, CommunityType.Recommand);
        }
        else
        {
            if (Time.time - m_lastInitTime < 60 && this.communityPanel.items.Count > 0)
                return;

            if (m_Data == null)
            {
                GameDebug.Log("CommunityPanel 重新获取moments数据");

                RequestServerController.instance.ReqMomentsData(m_Selection, 0, getMomentsNumOneTime, 0, (data) =>
                {
                    if (data != null)
                    {
                        GameDebug.Log("CommunityPanel -> InitUI 1");
                        m_lastInitTime = Time.time;
                        inited = true;
                        communityPanel.Init(data.userMoments);
                    }
                    else
                    {
                        GameDebug.LogError("CommunityPanel data is null");
                    }
                });
            }
            else
            {
                GameDebug.Log("CommunityPanel -> InitUI 2");
                m_lastInitTime = Time.time;
                inited = true;
                communityPanel.Init(m_Data.userMoments);
                m_Data = null;
            }
        }
    }

    private void DoOnSelectTopToggle(int type)
    {
        GameDebug.Log($"DoOnSelectTopToggle:{type}");

        if (m_Selection != type)
        {
            m_Selection = type;

            m_lastInitTime = -100;

            Init();
        }
    }

    public void OnClickAddMoment()
    {
        GameDebug.Log("CommunityUI -> OnClickAddMoment");

        GetComponentInParent<UIController>().ShowUI<AddMomentPanel>();
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            var rect = this.itemsParent.GetComponent<RectTransform>();

            float height = Mathf.Abs(rect.rect.height) - scollViewRect.GetComponent<RectTransform>().rect.height + 50;
            //GameDebug.Log($"height:{Mathf.Abs(rect.rect.height)}   view height:{scollViewRect.GetComponent<RectTransform>().rect.height}  safeHeight:{height}");

            if (Mathf.Abs(rect.anchoredPosition.y) > height)
            {
                RequestServerController.instance.ReqMomentsData(m_Selection, communityPanel.items.Count, getMomentsNumOneTime, 0,(data) =>
                {
                    GameDebug.Log($"CommunityUI -> Add:{data != null}");
                    if(data != null)
                        communityPanel.Add(data.userMoments);
                });
            }
            else if(rect.anchoredPosition.y < -150)
            {
                GameDebug.Log("CommunityUI -> Refresh");
                m_lastInitTime = -100;
                Init();
            }

        }
    }
}
